<?php

namespace app\api\model\mysql;

use app\common\model\TimeModel;

class MallCart extends TimeModel
{
    /**
     * 查询用户指定购物车商品
     * @param int $goods_id
     * @param int $option_id
     * @param string $openid
     * @return array|\think\Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getCartByGoodsId(int $goods_id=0,int $option_id=0,string $openid=''){
        return $this->field('*')
            ->where('openid',$openid)
            ->where('goods_id',$goods_id)
            ->where("option_id",$option_id)
            ->find();
    }

    /**
     * @param string $openid
     * @param int $cart_id
     * @return array|\think\Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getGoodsInfoByCartId(string $openid='',int $cart_id=0){
        return $this->field('*')
            ->where("openid",$openid)
            ->where("id",$cart_id)
            ->find();
    }
    /**
     * @param $ids
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function  getCartListByIds($ids){
        $cartids =explode(',',$ids);
        $cids =[];
        foreach ($cartids as $k=>$v){
            $cids[] =$v;
        }
        return $this->field('id,goods_id,goods_name as title,image,thumb_title,option_id,option_name,goods_price as price,total,option_name as get_specs_value_ids')
            ->where([['id','in',$cids]])
            ->select()
            ->order('id desc')
            ->toArray();
    }

    /**
     * 删除指定购物车id
     * @param $ids
     * @return bool
     */
    public function deleteByCartId($ids){
        $cartids =explode(',',$ids);
        $cids =[];
        foreach ($cartids as $k=>$v){
            $cids[] =$v;
        }
        return $this->where([['id','in',$cids]])->delete();
    }

    /**
     *  获取用户购物车信息
     * @param $openid
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getCartList($openid){
        return $this->field('id,goods_name,image,option_name,goods_price,total,option_name as get_specs_value_ids')
            ->where("openid",$openid)
            ->select()
            ->order('id desc')
            ->toArray();
    }


    /**
     *
     * @param $value
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    protected function getGetSpecsValueIdsAttr($value){
        if(empty($value)){
            return '';
        }
        $ids = explode("-",$value);
        $res= (new MallSpecsValue())->where('id','in',$ids)
            ->select()->toArray();
        return array_column($res,'title');
    }

}